﻿using Jw.Core;
using Jw.Models;
using Jw.Web.Areas.Dev.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace Jw.Web.Areas.Dev.Controllers
{
    public class SbgzyyController : Controller
    {
        /// <summary>
        /// 设备故障
        /// </summary>
        public ActionResult Index()
        {
            var codeService = new sys_codeService();
            var model = new
            {
                idField = "Sbgzyy_Yybh",
                urls = DevHelper.GetIndexUrls("Sbgzyy"),
                resx = DevHelper.GetIndexResx("设备故障原因"),
                dataSource = new
                {
                },
                form = new
                {
                    Sbgzyy_Yybh = ""
                },
                defaultRow = new
                {
                },
                setting = new
                {
                    postListFields = new string[] { "Sbgzyy_Yybh", "Sbgzyy_Yymc", "Sbgzyy_Lbbh", "Sbgzyy_Yyms", "Sbgzyy_Remark" }
                }
            };
            return View(model);
        }

    }


    public class SbgzyyApiController : ApiController
    {
        /// <summary>
        /// 设备故障-获取最新编号
        /// </summary>
        /// <returns></returns>
        public string getnewbillno()
        {
            return new b_SbgzyyService().GetNewKey("Sbgzyy_Yybh", "dateplus");
        }

        /// <summary>
        /// 获取类别编号
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public string getnewcode(string id)
        {
            return new b_SbgzyyService().GetNewCode("Sbgzyy_Yybh", "Sbgzyy_Lbbh", id);
        }
        /// <summary>
        /// 设备故障-获取
        /// </summary>
        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Sbgzyy_Yybh DESC'>
            <select>*</select>
                <from>b_Sbgzyy</from>
                    <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                        <field name='Sbgzyy_Yybh' cp='like'></field>
                    </where>
            </settings>");
            var service = new b_SbgzyyService();
            var pQuery = query.ToParamQuery();
            var result = service.GetDynamicListWithPaging(pQuery);

            return result;
        }
        /// <summary>
        /// 设备故障-编辑
        /// </summary>
        [System.Web.Http.HttpPost]
        public void Edit(dynamic data)
        {
            var formWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Sbgzyy_Yybh' cp='equal'></field></where>
            </settings>", typeof(b_Sbgzyy).Name);

            var result = new b_SbgzyyService().Edit(null, formWrapper, data);
        }
        /// <summary>
        /// 设备故障- 删除（删除主表前是删除明细)
        /// </summary>
        public void Delete(string id)
        {
            var result = new b_SbgzyyService().Delete(ParamDelete.Instance().AndWhere("Sbgzyy_Yybh", id));
            DevHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Sbda_Sbbm={0}]，请重试或联系管理员！", id);
        }

        #region 设备类别
        /// <summary>
        /// 设备故障类别-查询 
        /// </summary>
        public dynamic GetTypes(RequestWrapper request)
        {
            request.LoadSettingXmlString(@"
            <settings defaultOrderBy='Sbgzlb_Lbbh'>
               <where defaultIgnoreEmpty='true'>
                    <field name='Sbgzlb_Lbbh'  cp='equal'></field>
                    <field name='Sbgzlb_Lbmc'  cp='like' ></field>
                </where>
            </settings>
            ");
            var result = new b_SbgzlbService().GetDynamicList(request.ToParamQuery());
            return result;
        }
        /// <summary>
        /// 设备故障类别-编辑
        /// </summary>
        [System.Web.Http.HttpPost]
        public void EditType(dynamic data)
        {
            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>b_Sbgzlb</table>
                <where>
                    <field name='Sbgzlb_Lbbh' cp='equal'></field>
                </where>
            </settings>");
            var service = new b_SbgzlbService();
            var result = service.Edit(null, listWrapper, data);
        }
        #endregion
    }
}
